package uhaleks;

/**
 * Created by Александр on 02.07.2014.
 *
 * Класс "Дробное число"
 *
 */
public class Fractions {
    Fractions(long left_part) { left = left_part; }
    Fractions(int left_part, int right_part) { left = left_part; right = (short)right_part; }
    Fractions(long left_part, short right_part) { left = left_part; right = right_part; }
    long getLeft() { return left; }
    short getRight() { return right; }
    public Fractions sum(Fractions fractions) {
        long l = left + fractions.getLeft();
        short r = multiplication(right);
        r += multiplication(fractions.getRight());
        l += r / 10000;
        if (r >= 10000) r -= 10000;
        return new Fractions(l, r);
    }
    public Fractions sub(Fractions fractions) {
        long l = left - fractions.getLeft();
        short r = multiplication(right);
        r -= multiplication(fractions.getRight());
        l -= r / 10000;
        if (r >= 10000) r -= 10000;
        return new Fractions(l, r);
    }
    /*public Fractions Sum2(Fractions fractions) {
        double factor1 = (left > 0) ? 1 : -1;
        double factor2 = (fractions.getInteger() > 0) ? 1 : -1;
        double res = left + factor1 * division(right) + fractions.getInteger() + factor2 * division(fractions.getFract());
        return new Fractions( (long)(Math.floor(res)), (short)((res - (short)res) * 10) );
    }*/
    static double division(long i) {
        double d = i;
        while (d >= 1)
            d /= 10;
        return d;
    }
    static short multiplication(short i) {
        while (i < 1000)
            i *= 10;
        return i;
    }
    public String toString() {
        return Long.toString(left) + "," + Short.toString(right);
    }
    private long left;
    private short right;
}